(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.dynaparm.updateContinueController', updateContinueController);
	updateContinueController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'dynaparmService' ];
	function updateContinueController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, dynaparmService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.validatorConfig = validatorConfig;
		ctrl.dynaparm = {};
		ctrl.dynaparm.parmValues = ctrl.dynaparm.parmValues || [];
		ctrl.dynaparm.dynaparmSubCat = {};
		dynaparmService.initCtrlFunction(ctrl);
		
		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, layoutConfig.formConfigs.paramValues.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.dynaparm.parmValues';
			authService.updateContinue(params).then(function(response) {
				if (response.data && response.data.model && response.data.task) {
					ctrl.dynaparm = response.data.model || {};
					ctrl.dynaparm.taskId = response.data.task.taskId;
					ctrl.dynaparm.parmValues = ctrl.dynaparm.parmValues || [];
					dynaparmService.parseParmLength(ctrl.dynaparm);
					ctrl.changeServiceCode(ctrl.dynaparm);
					ctrl.changeParmCat(ctrl.dynaparm);
					ctrl.changedSubCat(ctrl.dynaparm);
					ctrl.changeParmTypeSubFun(ctrl.dynaparm);
					ctrl.changeParmValueUitype(ctrl.dynaparm);
					ctrl.layoutConfig = layoutConfig;
				}
			}, angular.noop);
		};

		ctrl.updateContinueSave = function() {
			dynaparmService.initParmLength(ctrl.dynaparm);
			return authService.updateContinueSave(ctrl.dynaparm).then(saveSuccessful, saveFailed);
		};

		ctrl.updateContinueSubmit = function() {
			dynaparmService.initParmLength(ctrl.dynaparm);
			return authService.updateContinueSubmit(ctrl.dynaparm).then(submitSuccessful, submitFailed);
		};

		ctrl.updateContinueCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.dynaparm.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateContinueCancel(ctrl.dynaparm).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedSubCommpanyList = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.dynaparm.list',
					'state' : 'cda.dynaparm.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.dynaparm = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.dynaparm.list',
					'state' : 'cda.dynaparm.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);